
import AbsWindow from "../../framework/AbsWindow";
import { WindowName } from "../../framework/reflect/WindowName";
import GameWindow from "./GameWindow";
import Context from "../../framework/Context";
import LocalStorage from "../../framework/utils/LocalStorage";
import LoadResData from "../../framework/LoadResData";
import FinishWindow from "./FinishWindow";
const { ccclass, property } = cc._decorator;
@ccclass
@WindowName("SettlementWindow")
export default class SettlementWindow extends AbsWindow {

    @property(cc.Node)
    private returnBtn: cc.Node = null;
    @property(cc.Node)
    private rightNode: cc.Node = null;
    @property(cc.Node)
    private combatantNode: cc.Node = null;
    @property(cc.Node)
    private fluencyNode: cc.Node = null;
    @property(cc.Node)
    private superNode: cc.Node = null;
    @property(cc.Node)
    private wrongNode: cc.Node = null;
    @property(cc.Label)
    private weaponName: cc.Label = null;
    @property(cc.Label)
    private weaponProperty: cc.Label = null;
    private rightTotalNum = 0;
    private wrongTotalNum = 0;
    private fluencyTotalNum = 0;
    private combatantTotalNum = 0;
    private superTotalNum = 10;


    private count:number = 0;
    private curWeaponData = null;
    public static sendData(data, rightNum: number, wrongNum: number, fluencyNum: number, combatantNum: number, superNum: number,combatantTotalCount:number): Promise<SettlementWindow> {
        return Context.get().startWindow(SettlementWindow).then(settlement => {
            settlement.initData(data, rightNum, wrongNum, fluencyNum, combatantNum, superNum,combatantTotalCount);
            cc.log("SettlementWindow sendData", data, rightNum, wrongNum, fluencyNum, combatantNum, superNum);
            return Promise.resolve(settlement)
        })
    }
    public initData(data, rightNum: number, wrongNum: number, fluencyNum: number, combatantNum: number, superNum: number,combatantTotalCount:number) {
        this.curWeaponData = data;
        this.weaponName.string = data[0]["name"];
        this.weaponProperty.string = "+" + data[1] + data[0]["name"];
        this.rightNode.getChildByName("label").getComponent(cc.Label).string = String(rightNum);
        this.combatantNode.getChildByName("label").getComponent(cc.Label).string = String(combatantNum);
        this.fluencyNode.getChildByName("label").getComponent(cc.Label).string = String(fluencyNum);
        this.superNode.getChildByName("label").getComponent(cc.Label).string = String(superNum);
        this.wrongNode.getChildByName("label").getComponent(cc.Label).string = String(wrongNum);
        //目标滚动条
        var lv = Number(data[1]);
        var knockingData = LoadResData.knockingData;
        var first = 0, second = 0, third = 0;
        var targetRightNum = 0, targetFluencyNum = 0, targetSuperNum = 0, targetcombatantNum = 0;;
        for (let i = 0; i < knockingData.length; i++) {
            if (lv == knockingData[i]["lv"]) {
                first = knockingData[i]["first"];
                second = knockingData[i]["second"];
                third = knockingData[i]["third"];
                targetRightNum = knockingData[i]["right"];
                targetFluencyNum = knockingData[i]["liuchang"];
                targetSuperNum = knockingData[i]["super"];
                targetcombatantNum = knockingData[i]["commob"];
                this.rightTotalNum = first * 4 + second * 3 + third * 3;
                this.combatantTotalNum = first * 4 + second * 3 + third * 3;
                this.wrongTotalNum = first * 4 + second * 3 + third * 3;
                this.fluencyTotalNum = (first - 1) * 4 + (second - 1) * 3 + (third - 1) * 3;

            }
        }
        if (rightNum < targetRightNum || fluencyNum < targetFluencyNum
           || superNum < targetSuperNum) {
            this.weaponProperty.string = "瑕疵的+" + data[1] + data[0]["name"];
        }
        else {
            this.weaponProperty.string = "完美的+" + data[1] + data[0]["name"];
            var weaponLV = Number(data[1]) + 1;

            this.changeWeaponData(data[0]["id"], weaponLV);
        }
        var rightProgressBar = this.rightNode.getChildByName("baseProgressBar").getComponent(cc.ProgressBar);
        rightProgressBar.progress = targetRightNum / this.rightTotalNum;

        var comProgressBar = this.combatantNode.getChildByName("baseProgressBar").getComponent(cc.ProgressBar);
        comProgressBar.progress = targetcombatantNum / this.combatantTotalNum;

        var fluencyProgressBar = this.fluencyNode.getChildByName("baseProgressBar").getComponent(cc.ProgressBar);
        fluencyProgressBar.progress = targetFluencyNum / this.fluencyTotalNum;

        var superProgressBar = this.superNode.getChildByName("baseProgressBar").getComponent(cc.ProgressBar);
        superProgressBar.progress = targetSuperNum / this.superTotalNum;

        this.progressBarAnimation(rightNum, wrongNum, fluencyNum, combatantNum, superNum);
        this.totalCount(rightNum, wrongNum, fluencyNum, combatantNum, superNum,combatantTotalCount);
    }
    public onStart() {
        this.setClickListener(this.returnBtn, this.onReturnBtn);
    }

    public listen(): string[] {
        return []
    }
    public handleMessage(message: string, args: any[]): boolean {
        switch (message) {
        }
        return false
    }
    private onReturnBtn() {
        this.context.closeWindow(SettlementWindow);
        FinishWindow.sendData(this.curWeaponData, this.count);
    }
    private totalCount(rightNum: number, wrongNum: number, fluencyNum: number, combatantNum: number, superNum: number,combatantTotalCount:number) {
        var weapons = LoadResData.weaponsData;
        var curWeaponLv: any = LocalStorage.getObj("weaponLv");
        var rightBaseCount = 0, fluencyBaseCount = 0, combatantBaseCount = 0, superBaseCount = 0;
        for (let i in curWeaponLv) {
            for (let j in weapons) {
                if (curWeaponLv[i]["id"] == weapons[j]['skill_type']) {
                    switch (weapons[j]['skill_type']) {
                        case 2: {
                            superBaseCount += Number(curWeaponLv[i]['lv']) * Number(weapons[j]['skill_base']);
                            break;
                        }
                        case 3: {
                            rightBaseCount += Number(curWeaponLv[i]['lv']) * Number(weapons[j]['skill_base']);
                            break;
                        }
                        case 4: {
                            fluencyBaseCount += Number(curWeaponLv[i]['lv']) * Number(weapons[j]['skill_base']);
                            break;
                        }
                    }
                }
            }
        }
        this.count = rightBaseCount * rightNum + fluencyBaseCount * fluencyNum + combatantTotalCount + superBaseCount * superNum;
    }
    //实际数量滚动条
    private progressBarAnimation(rightNum: number, wrongNum: number, fluencyNum: number, combatantNum: number, superNum: number) {
        var rightProgressBar = this.rightNode.getChildByName("curProgressBar").getComponent(cc.ProgressBar);
        rightProgressBar.progress = rightNum / this.rightTotalNum;
        this.playeAnimation_right(0, rightNum, this.rightTotalNum, rightProgressBar);

        var comProgressBar = this.combatantNode.getChildByName("curProgressBar").getComponent(cc.ProgressBar);
        comProgressBar.progress = combatantNum / this.combatantTotalNum;
        this.playeAnimation_combatant(0, combatantNum, comProgressBar.totalLength, comProgressBar);

        var fluencyProgressBar = this.fluencyNode.getChildByName("curProgressBar").getComponent(cc.ProgressBar);
        fluencyProgressBar.progress = fluencyNum / this.fluencyTotalNum;
        this.playeAnimation_fluency(0, fluencyNum, this.fluencyTotalNum, fluencyProgressBar);

        var superProgressBar = this.superNode.getChildByName("curProgressBar").getComponent(cc.ProgressBar);
        superProgressBar.progress = superNum / this.superTotalNum;
        this.playeAnimation_super(0, superNum, this.superTotalNum, superProgressBar);

        var wrongProgressBar = this.wrongNode.getChildByName("curProgressBar").getComponent(cc.ProgressBar);
        wrongProgressBar.progress = wrongNum / this.wrongTotalNum;
        this.playeAnimation_wrong(0, wrongNum, this.wrongTotalNum, wrongProgressBar);
    }
    private playeAnimation_right(num, rightNum, totalLength, ProgressBar) {
        if (num <= 0 || num >= rightNum) {
            return;
        }
        ProgressBar.progress = num / totalLength;
        num++
        var self = this;
        setTimeout(() => {
            self.playeAnimation_right(num, rightNum, totalLength, ProgressBar)
        }, 500);
    }
    private playeAnimation_combatant(num, combatantNum, totalLength, ProgressBar) {
        if (num <= 0 || num >= combatantNum) {
            return;
        }
        ProgressBar.progress = num / totalLength;
        num++
        var self = this;
        setTimeout(() => {
            self.playeAnimation_combatant(num, combatantNum, totalLength, ProgressBar)
        }, 500);
    }
    private playeAnimation_fluency(num, fluencyNum, totalLength, ProgressBar) {
        if (num <= 0 || num >= fluencyNum) {
            return;
        }
        ProgressBar.progress = num / totalLength;
        num++
        var self = this;
        setTimeout(() => {
            self.playeAnimation_fluency(num, fluencyNum, totalLength, ProgressBar)
        }, 500);
    }
    private playeAnimation_super(num, superNum, totalLength, ProgressBar) {
        if (num <= 0 || num >= superNum) {
            return;
        }
        ProgressBar.progress = num / totalLength;
        num++
        var self = this;
        setTimeout(() => {
            self.playeAnimation_super(num, superNum, totalLength, ProgressBar)
        }, 500);
    }
    private playeAnimation_wrong(num, wrongNum, totalLength, ProgressBar) {
        if (num <= 0 || num >= wrongNum) {
            return;
        }
        ProgressBar.progress = num / totalLength;
        num++
        var self = this;
        setTimeout(() => {
            self.playeAnimation_wrong(num, wrongNum, totalLength, ProgressBar)
        }, 500);
    }
    private changeWeaponData(id, lv) {
        var weaponLv: any = LocalStorage.getObj("weaponLv");
        for (let index in weaponLv) {
            if (weaponLv[index]["id"] == id) {
                var data = { "id": id, "lv": lv };
                weaponLv.splice(Number(index), 1, data);
            }
        }
        LocalStorage.setObj("weaponLv", weaponLv);
    }
}